package com.azxc.rapid.common.utils;

import com.azxc.rapid.common.enums.DictEnum;
import com.azxc.rapid.system.cache.DictCache;
import com.azxc.rapid.system.entity.Dict;
import lombok.experimental.UtilityClass;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@UtilityClass
public class DictUtil {

	public static String getDescription(DictEnum en, String key) {
		if (StringUtils.isEmpty(key)) {
			return "";
		}
		List<Dict> dictList = DictCache.getList(en.getValue());
		Assert.notEmpty(dictList, "系统提示：字典值[" + en.getValue() + "]未定义，请联系管理员进行新增！");
		Map<String, String> dictMap = dictList.stream().collect(Collectors.toMap(Dict::getDictKey, Dict::getDictValue));
		return dictMap.get(key);
	}

	public static Map<String, String> getDictKeyValueMap(DictEnum en) {
		List<Dict> dictList = DictCache.getList(en.getValue());
		Assert.notEmpty(dictList, "系统提示：字典值[" + en.getValue() + "]未定义，请联系管理员进行新增！");
		Map<String, String> dictMap = dictList.stream().collect(Collectors.toMap(Dict::getDictKey, Dict::getDictValue));
		return dictMap;
	}

	public static Map<String, String> getDictValueKeyMap(DictEnum en) {
		List<Dict> dictList = DictCache.getList(en.getValue());
		Assert.notEmpty(dictList, "系统提示：字典值[" + en.getValue() + "]未定义，请联系管理员进行新增！");
		Map<String, String> dictMap = dictList.stream().collect(Collectors.toMap(Dict::getDictValue, Dict::getDictKey));
		return dictMap;
	}
}
